package com.bw.common.mvi.state

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update


class RealContainer<STATE : UiState>(initialState: STATE) : MutableContainer<STATE> {

    private val _requestStateFlow = MutableStateFlow(
        RequestState(request = false,complate = false,erro = false,
        refresh = false,load = false)
    )

    private val _uiStateFlow  = MutableStateFlow(initialState)

    override val requestStateFlow: StateFlow<RequestState> = _requestStateFlow

    override val uiStateFlow: StateFlow<STATE> = _uiStateFlow

    override fun updateRequestState(action: RequestState.() -> RequestState) {
        _requestStateFlow.update { action(_requestStateFlow.value) }
    }

    override fun updateUiState(action: STATE.() -> STATE) {
        _uiStateFlow.update { action(_uiStateFlow.value) }
    }
}